package com.nl.sort;

import java.util.Arrays;

public class EvenOddPartition {

	
	public static interface ReorderRule{
		boolean match(int value);
	}

	public static void reorder(int[] arr,ReorderRule rule){
		if(arr==null || arr.length ==0 ){
			return;
		}
		int left=0,right=arr.length-1;
		
		while(left<right){
			//遇到偶数
			while(left<right&&rule.match(arr[right])){
				right--;
			}

			//遇到奇数
			while(left<right&&!rule.match(arr[left])){
				left++;
			}
			if(left<right){
				int tmp = arr[left];
				arr[left]=arr[right];
				arr[right] = tmp;
			}
		}
	}
	
	public static void main(String[] args) {
		int[] arr = new int[]{12,3,4,5,1,342};

		reorder(arr,new ReorderRule() {
			@Override
			public boolean match(int value) {
				return (value&0x1)==0;
			}
		});

		System.out.println(Arrays.toString(arr));
	}
}
